<template>
  <div>
    <h1>商品列表</h1>
    <table>
      <thead>
        <tr>
          <td>商品名称</td>
          <td>商品图片</td>
          <td>商品价格</td>
          <td>操作</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.store_name }}</td>
          <td><img :src="item.image" width="80px" /></td>
          <td>{{ item.price }}</td>
          <td>
            <button @click="addCart(item)">加入购物车</button>
            <button>收藏</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
    };
  },
  methods: {
    addCart(item){  
      let cartItem={
        ...item,
        count:1
      }
      this.$store.commit('ADD_CART',cartItem)
    },
    async getProductList() {
      const result = await this.$api.product.getProudctListApi();
      this.list = result.data.data.list;
    },
  },
  created() {
    this.getProductList();
  },
};
</script>

<style lang="scss" scope>
  table{
    width: 100%;
    border-collapse: collapse;
  }
  td{
    padding:10px;
    border:1px solid #ccc;
  }
</style>